<?php


namespace app\common\model\tang;

use app\admin\model\AdminUser;
use app\common\impl\Comm;
use app\common\model\CommonBaseModel;
use app\common\model\tang\Merchant;
use think\Facade\Db;

class Principal extends CommonBaseModel implements Comm
{

    public function getAdminPageData($param = [], $order = 'id desc',$where=[])
    {
        $model = self::where($where);
        $adminModel = new AdminUser();
        $merchantModel = new \app\common\model\tang\Merchant();
        if(isset($param['section_id'])&&!empty($param['section_id'])){
            $model->where('section_id','=',$param['section_id']);
        }

        $data = $model->order($order)->with(['adminUser'])->where($where)->page(PAGE)->limit(LIMIT)->select()->toArray();
        foreach ($data as $k=>$v){
            if(!empty($v['principal'])){
                $data[$k]['principal_name'] = $adminModel->whereIn('id',explode(',',$v['principal']))->column('nickname');
            }
            if(!empty($v['merchant_ids'])){
                $data[$k]['merchant_name'] = $merchantModel->whereIn('id',explode(',',$v['merchant_ids']))->column('name');
            }
        }
        $count = count($data);
        return parent::ajaxResult($data,$count);

    }
    public function merchant(): \think\model\relation\HasOne
    {
        return $this->hasOne(Merchant::class,'id','merchant_id');
    }
    public function delById($id)
    {
        // TODO: Implement delById() method.
    }

    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
    public function adminUser(){
        return $this->hasOne(AdminUser::class,'id','principal_id')->bind([
            'name'=>'nickname'
        ]);
    }
}